﻿package Welsy4.Connect.vo {
	import flash.events.ErrorEvent;

	import mx.formatters.DateFormatter;


	/**
	 * ...
	 * @author DefaultUser (Tools -> Custom Arguments...)
	 */
	public class Calendar {

		public function Calendar() {

		}

		public static function generateUTCdateFromTimestamp() : void {

		}

		/**
		 * restiruisce una stringa "YYYY-MM-DDTh:m" es. 2006-10-01T17:00
		 * @return data odierna per Adobe Connect
		 */
		public static function getAdobeConnectDate(d : Date) : String {
			var s : String = "";
			s = d.getUTCFullYear() + "-" + addZero(d.getUTCMonth() + 1) + "-" + addZero(d.getUTCDate()) + "T" + addZero(d.getUTCHours()) + ":" + addZero(d.getUTCMinutes());
			/* 		 var formatter : DateFormatter = new DateFormatter();
			   formatter.formatString = "YYYY-MM-DDTJJ:NN";
			 s = formatter.format(d);  */
			return s;
		}

		/**
		 * 012345678910|11|13|15|
		 * YYYY-MM-DDT |hh| :|mm|
		 * @param s
		 * @return
		 *
		 */
		public static function getAdobeConnectDateFromString(s : String) : Date {
			var d : Date = new Date();
			var i : int = 0;
			var sTMP : String;

			/* 	sTMP = s.substr(i, s.length); */
			var formatter : DateFormatter = new DateFormatter();
			formatter.formatString = "YYYY-MM-DDTJJ:NN";


			try {
				sTMP = s.substring(i, i += 4);
				d.fullYearUTC = Number(sTMP);
				sTMP = s.substring(i += 1, i += 2);
				d.monthUTC = Number(sTMP)-1;
				sTMP = s.substring(i += 1, i += 2);
				d.dateUTC = Number(sTMP);
				sTMP = s.substring(i += 1, i += 2);
				d.hoursUTC = Number(sTMP);
				sTMP = s.substring(i += 1, i += 2)
				d.minutesUTC = Number(sTMP);
			}
			catch (err : ErrorEvent) {
				trace(err.text);
			}
			trace(formatter.format(d));
			return d;
		}

		public static function addZero(n : Number) : String {
			if (n < 10) {
				return "0" + n;
			}
			return "" + n;
		}
	}

}

